package com.sharemarking.application.wechat.service;

import com.sharemarking.application.common.api.ResponseParams;
import com.sharemarking.application.common.utils.StringUtils;
import com.sharemarking.application.wechat.entity.Bless;
import com.sharemarking.application.wechat.entity.Client;
import com.sharemarking.application.wechat.repository.BlessRepo;
import com.sharemarking.application.wechat.repository.ClientRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;


/**
 * Created by Administrator on 2017/10/13.
 */
@Service
@Transactional
public class BlessService {

    @Autowired
    ClientRepo clientRepo;

    @Autowired
    BlessRepo blessRepo;

    /**
     * 新增祝福
     * @param request
     * @return
     */
    public ResponseParams addBless(HttpServletRequest request) {
        ResponseParams responseParams = new ResponseParams();
        Client fromClient;
        Client toClient;
        String bText;
        String bImage;
        String bAudio;
        String bVideo;

        try {
            if (StringUtils.isEmpty(request.getParameter("fromClientId"))){
                responseParams.isRequiredParamsIsMissing("fromClientId缺失");
                return responseParams;
            }
        } catch (Exception e) {
            e.printStackTrace();
            responseParams.isRequiredParamsError();
            return responseParams;
        }
        fromClient = clientRepo.findOne(Integer.parseInt(request.getParameter("fromClientId")));
        Bless bless = new Bless();
        bless.setFromClient(fromClient);
        if (StringUtils.isNotEmpty(request.getParameter("toClientId"))){
            toClient = clientRepo.findOne(Integer.parseInt(request.getParameter("toClientId")));
            bless.setToClient(toClient);
        }
        bText = request.getParameter("bText");
        if (StringUtils.isNotEmpty(bText)){
            bless.setbText(bText);
        }
        bImage = request.getParameter("bImage");
        if (StringUtils.isNotEmpty(bImage)){
            bless.setbImage(bImage);
        }
        bAudio = request.getParameter("bAudio");
        if (StringUtils.isNotEmpty("bAudio")){
            bless.setbAudio(bAudio);
        }
        bVideo = request.getParameter("bVideo");
        if (StringUtils.isNotEmpty("bVideo")){
            bless.setbVideo(bVideo);
        }
        blessRepo.save(bless);
        return responseParams;
    }


    public ResponseParams upload(HttpServletRequest request) {
        ResponseParams responseParams = new ResponseParams();
        return responseParams;
    }
}
